//
//  ScanMaskView.m
//  ScanQRCode
//
//  Created by JiaCong Zhu on 2019/1/23.
//  Copyright © 2019 XiMu. All rights reserved.
//


#import "ScanMaskView.h"
#import "UIColor+EX.h"

/***  屏幕宽 */
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
/***  屏幕宽高比例 */
#define SCALE(s)                          ((s) / 375.0 * kScreenWidth)
/***  1pt像素点宽度 */
#define k1ptWidth (1.1 / [UIScreen mainScreen].scale)

@implementation ScanMaskView

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    // 剧中260*260透明框
    CGPoint point1 = CGPointMake(rect.size.width / 2 - SCALE(130), rect.size.height / 2 - SCALE(130));
    CGPoint point2 = CGPointMake(rect.size.width / 2 + SCALE(130), rect.size.height / 2 - SCALE(130));
    CGPoint point3 = CGPointMake(rect.size.width / 2 - SCALE(130), rect.size.height / 2 + SCALE(130));
    CGPoint point4 = CGPointMake(rect.size.width / 2 + SCALE(130), rect.size.height / 2 + SCALE(130));
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:0 alpha:0.36].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextMoveToPoint(ctx, point1.x, point1.y);
    CGContextAddLineToPoint(ctx, point2.x, point2.y);
    CGContextAddLineToPoint(ctx, point4.x, point4.y);
    CGContextAddLineToPoint(ctx, point3.x, point3.y);
    CGContextAddLineToPoint(ctx, point1.x, point1.y);
    CGContextAddLineToPoint(ctx, 0, 0);
    CGContextAddLineToPoint(ctx, 0, CGRectGetMaxY(rect));
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), 0);
    CGContextAddLineToPoint(ctx, 0, 0);
    CGContextDrawPath(ctx, kCGPathFill);
    
    // 白色边框
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, point1.x, point1.y);
    CGContextAddLineToPoint(ctx, point2.x, point2.y);
    CGContextAddLineToPoint(ctx, point4.x, point4.y);
    CGContextAddLineToPoint(ctx, point3.x, point3.y);
    CGContextAddLineToPoint(ctx, point1.x, point1.y);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(ctx, k1ptWidth);
    CGContextDrawPath(ctx, kCGPathStroke);
    
    // 绿色角
    CGFloat length = SCALE(20);
    CGFloat lineWidth = SCALE(4);
    CGFloat offset = lineWidth / 2 - k1ptWidth;
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, point1.x + offset, point1.y + length + offset);
    CGContextAddLineToPoint(ctx, point1.x + offset, point1.y + offset);
    CGContextAddLineToPoint(ctx, point1.x + length + offset, point1.y + offset);
    
    CGContextMoveToPoint(ctx, point2.x - offset, point2.y + length + offset);
    CGContextAddLineToPoint(ctx, point2.x - offset, point2.y + offset);
    CGContextAddLineToPoint(ctx, point2.x - length - offset, point2.y + offset);
    
    CGContextMoveToPoint(ctx, point3.x + offset, point3.y - length - offset);
    CGContextAddLineToPoint(ctx, point3.x + offset, point3.y - offset);
    CGContextAddLineToPoint(ctx, point3.x + length + offset, point3.y - offset);
    
    CGContextMoveToPoint(ctx, point4.x - offset, point4.y - length - offset);
    CGContextAddLineToPoint(ctx, point4.x - offset, point4.y - offset);
    CGContextAddLineToPoint(ctx, point4.x - length - offset, point4.y - offset);
    
    CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithHexString:@"33B87C"].CGColor);
    CGContextSetLineWidth(ctx, lineWidth);
    CGContextDrawPath(ctx, kCGPathStroke);
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

@end
